package com.deruan.hotcore.resource.table.plugin;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;

import org.columba.core.gui.base.AscendingIcon;
import org.columba.core.gui.base.DescendingIcon;

import com.deruan.hotcore.resource.table.model.ResourceTableModelSorter;

public class ResourceBasicRenderer extends DefaultTableCellRenderer
{
	    private ResourceTableModelSorter sorter;
	    private ImageIcon ascending = new AscendingIcon();
	    private ImageIcon descending = new DescendingIcon();
	    private String name;
	    
	    public ResourceBasicRenderer(String name, ResourceTableModelSorter sorter) {
	        super();

	        this.name = name;
	        this.sorter = sorter;

	        setHorizontalAlignment(SwingConstants.LEFT);
	        setHorizontalTextPosition(SwingConstants.LEFT);
	    }
	    

	    public Component getTableCellRendererComponent(JTable table, Object value,
	        boolean isSelected, boolean hasFocus, int row, int column) {
	        if (table != null) {
	            JTableHeader header = table.getTableHeader();

	            if (header != null) {
	                setForeground(header.getForeground());
	                setBackground(header.getBackground());
	                setFont(header.getFont());
	            }
	        }

	        setText((value == null) ? "" : value.toString());
	        setBorder(UIManager.getBorder("TableHeader.cellBorder"));

	        if (sorter.getSortingColumn().equals(name)) {
	            if (sorter.getSortingOrder()) {
	                setIcon(descending);
	            } else {
	                setIcon(ascending);
	            }
	        } else {
	            setIcon(null);
	        }

	        return this;
	    }
}
